package net.ojbk.timeSpace.test.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import net.ojbk.timeSpace.test.beans.Img;
import net.ojbk.timeSpace.test.common.sgz.LocalAddress;
import net.ojbk.timeSpace.test.dao.ImgDao;
import net.ojbk.timeSpace.test.service.IImgService;

@Service("imgService")
public class ImgServiceImpl implements IImgService {

	@Autowired
	@Qualifier("imgDao")
	ImgDao imgDao;

	@Override
	public byte[] getImg(String imgUUID) {
		// TODO Auto-generated method stub

		File imgFile = new File(LocalAddress.imgAddress + File.separator + imgUUID + ".jpg");

		byte[] data = null;
		FileInputStream fileInputStream = null;
		ByteArrayOutputStream byteArrayOutputStream = null;

		try {
			fileInputStream = new FileInputStream(imgFile);
			byteArrayOutputStream = new ByteArrayOutputStream((int) imgFile.length());
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = fileInputStream.read(buffer)) != -1) {
				byteArrayOutputStream.write(buffer, 0, len);
			}
			data = byteArrayOutputStream.toByteArray();
			fileInputStream.close();
			byteArrayOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public List<Img> getAllImgByTimeBladeId(int timeBladeId) {
		return imgDao.selectAllRecordByTimeBladeId(timeBladeId);
	}

	@Override
	public void addImg(Img img) {
		imgDao.insertOneRecord(img);
	}

}
